//
//  DataUtils.m
//  yilutongxing
//
//  Created by T&J on 2017/5/29.
//  Copyright © 2017年 YZE. All rights reserved.
//
#import "DataUtils.h"
#import <Foundation/Foundation.h>
#import "MKJChatModel.h"
#import "UserDefaultsTools.h"
#import "TimeTools.h"


@implementation DataUtils

+(NSString *)getDocumentsPath{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path = [paths objectAtIndex:0];
    return path;
}

+(NSNumber*)getMyUID{
    NSDictionary *dic=[UserDefaultsTools getUserDefaultsForKey:@"userInfo"];
    if (dic) {
        return dic[@"user"][@"id"];
    }
    else {
        return nil;
    }
}

+(MKJChatModel*)getChatMdodelFromJsonObj:(id)obj selfUID:(NSNumber*)selfuid{
    MKJChatModel *model = [MKJChatModel mj_objectWithKeyValuesAdd:obj];
    if (selfuid==model.dSender.id) {
        //是自己发的
        model.isRight = YES;
    }
    else {
        model.isRight = NO;
    }
    return model;
}

+(BOOL)saveDataToPlist:(NSString*)plistname data:(id)data{
    NSString *path = [DataUtils getDocumentsPath];
    NSString *filename = [path stringByAppendingPathComponent:plistname];
    return [data writeToFile:filename atomically:YES];
}

+(NSString*)timeDescAfterOperation:(NSString *)opTime{
    if (!opTime || ![opTime isNotEmpty]) {
        return @"手术时间未填写";
    }
    NSString *val = @"";
    int month=0;
    int interval = [TimeTools getIntervalSecond:[[TimeTools getCurrentTimeYMD] stringByAppendingString:@" 08:00"] time2:[opTime stringByAppendingString:@" 08:00"]];
    interval = interval/60/600/24;
    if (interval<=15) {
        val = [NSString stringWithFormat:@"术后%d天",interval];
    }
    else if (interval<=30){
        val = @"术后半个月";
    }
    else {
        month = interval/30;
        int halfmonth = (interval%30)/15;
        if (halfmonth==0) {
            val = [NSString stringWithFormat:@"术后%d个月",month];
        }
        else {
            val = [NSString stringWithFormat:@"术后%d个半月",month];
        }
        
    }
    return val;
}

+ (BOOL)fileIfExists:(NSString*)filename{
    NSString *filePath = [[DataUtils getDocumentsPath] stringByAppendingPathComponent:filename];
    NSFileManager *manager = [NSFileManager defaultManager];
    return [manager fileExistsAtPath:filePath];
}

+ (NSString *)arabicConvert2Chinese:(NSUInteger)number{
    NSString *chnumber;
    switch (number) {
        case 0:{
            chnumber = @"零";
        }
            break;
        case 1:{
            chnumber = @"一";
        }
            break;
        case 2:{
            chnumber = @"二";
        }
            break;
        case 3:{
            chnumber = @"三";
        }
            break;
        case 4:{
            chnumber = @"四";
        }
            break;
        case 5:{
            chnumber = @"五";
        }
            break;
        case 6:{
            chnumber = @"六";
        }
            break;
        case 7:{
            chnumber = @"七";
        }
            break;
        case 8:{
            chnumber = @"八";
        }
            break;
        case 9:{
            chnumber = @"九";
        }
            break;
        default:
            break;
    }
    return chnumber;
}

+ (NSInteger)getIndicatorSortNumber:(NSString *)indicator_name{
    BOOL ret = [DataUtils fileIfExists:SINAME_CACHE];//本地是否有缓存
    if (!ret) {
        return -1;
    }
    //取缓存的字典
    NSArray *indicator_names = [[NSMutableArray alloc]initWithContentsOfFile:[[DataUtils getDocumentsPath] stringByAppendingPathComponent:SINAME_CACHE]];
    NSInteger index = 0;
    for (int i=1; i<=indicator_names.count; i++) {
        NSDictionary *node_parent = indicator_names[i-1];
        if ([node_parent[@"name"] isEqualToString:indicator_name]) {
            index = i*10000;
            break;
        }
        NSArray *sub_indicator_names = node_parent[@"specialIndicatorsDTOs"];
        for (int j=1; j<=sub_indicator_names.count; j++) {
            NSDictionary *node_child = sub_indicator_names[j-1];
            if ([node_child[@"name"] isEqualToString:indicator_name]) {
                index = i*10000 + j*1000;
                break;
            }
        }
        if (index>0) {
            break;
        }
    }
    
    return index;
}

+ (NSArray*)getTidyArrayFromString:(NSString *)string{
    NSMutableArray *toArr = [[string componentsSeparatedByString:@","] mutableCopy];
    NSEnumerator *en = [toArr reverseObjectEnumerator];
    NSString *item;
    while (item = [en nextObject]) {
        if ([item isEqualToString:@""] || ![item isNotEmpty]) {
            [toArr removeObject:item];
        }
    }
    return toArr;
}

@end

